﻿
using System.Collections.Generic;
using System;
namespace Rsdn.SmartApp.Configuration
{
	internal class VarsContainer
	{
		private readonly IDictionary<string, string> _varsDic;
		private readonly VarsContainer _parent;

		public VarsContainer(IDictionary<string, string> varsDic, VarsContainer parent)
		{
			if (varsDic == null)
				throw new ArgumentNullException("varsDic");
			_varsDic = varsDic;
			_parent = parent;
		}

		public VarsContainer(IDictionary<string, string> varsDic) : this(varsDic, null)
		{
		}

		public string GetVar(string name)
		{
			string value;
			if (_varsDic.TryGetValue(name, out value))
				return value;
			if (_parent != null)
				return _parent.GetVar(name);
			throw new ArgumentException("Variable '" + name + "' is not defined.");
		}

		public bool IsVarDefined(string name)
		{
			if (_varsDic.ContainsKey(name))
				return true;
			if (_parent != null)
				return _parent.IsVarDefined(name);
			return false;
		}
	}
}
